﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoveSmushboxConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Cove.Smushbox.Console v.{0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
            Console.WriteLine();

            if (args == null || args.Count() < 4)
            {
                PrintHelp();
            }
            else
            {
                try
                {
                    using (var smush = new CoveSmushbox.Smushbox(args[0]))
                    {
                        smush.Login(args[1], args[2]);
                        switch (args[3])
                        {
                            case "send":
                                if (args.Count() < 4 + 2)
                                {
                                    PrintHelp();
                                    break;
                                }

                                var message = args[5].Replace(@"\n", "\n");

                                smush.SendMessage(args[4], message);

                                Console.WriteLine("Message sent to \"" + args[4] + "\".");

                                break;

                            case "getIncoming":
                                var inMessages = smush.GetIncomingMessages();
                                foreach (var m in inMessages)
                                {
                                    if (args.Length >= 4 + 1 && args[4] == "json")
                                    {
                                        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(m));
                                    }
                                    else
                                    {
                                        Console.WriteLine(m.ToString());
                                    }
                                }
                                break;

                            case "getOutgoing":
                                var outMessages = smush.GetOutgoingMessages();
                                foreach (var m in outMessages)
                                {
                                    if (args.Length >= 4 + 1 && args[4] == "json")
                                    {
                                        Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(m));
                                    }
                                    else
                                    {
                                        Console.WriteLine(m.ToString());
                                    }
                                }
                                break;

                            case "deleteIncoming":
                                int messageId;
                                if (args.Count() < 4 + 1 || !int.TryParse(args[4], out messageId) || messageId < 0)
                                {
                                    PrintHelp();
                                    break;
                                }

                                smush.DeleteIncomingMessage(messageId);
                                Console.WriteLine("Incoming message \"" + messageId.ToString() + "\" deleted.");
                                break;

                            case "deleteOutgoing":
                                if (args.Count() < 4 + 1 || !int.TryParse(args[4], out messageId) || messageId < 0)
                                {
                                    PrintHelp();
                                    break;
                                }

                                smush.DeleteOutgoingMessage(messageId);
                                Console.WriteLine("Outgoing message \"" + messageId.ToString() + "\" deleted.");
                                break;

                            default:
                                PrintHelp();
                                break;
                        }
                    }
                
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
            Console.WriteLine();
            Console.WriteLine("Press ANY key to exit...");
            Console.WriteLine();
            Console.ReadKey(true);
        }

        static void PrintHelp()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("  Cove.Smushbox.Console address username password command [parameters,...]");
            Console.WriteLine();
            Console.WriteLine("Commands:");
            Console.WriteLine("  send - Sends a SMS message (use \n for new line on message).");
            Console.WriteLine("    send number message");
            Console.WriteLine();
            Console.WriteLine("  getIncoming - Lists incoming messages.");
            Console.WriteLine("    getIncoming [json]");
            Console.WriteLine();
            Console.WriteLine("  getOutgoing - Lists outgoing messages.");
            Console.WriteLine("    getOutgoing [json]");
            Console.WriteLine();
            Console.WriteLine("  deleteIncoming - Deletes an incoming messages.");
            Console.WriteLine("    deleteIncoming messageId");
            Console.WriteLine();
            Console.WriteLine("  deleteOutgoing - Deletes an outgoing messages.");
            Console.WriteLine("    deleteOutgoing messageId");
            Console.WriteLine();
            Console.WriteLine("Example:");
            Console.WriteLine("  Cove.Smushbox.Console 192.168.0.2 smushbox mypassword send 1-555-2424 \"A SMUSH Message!\"");
        }
    }
}
